package com.lagou.worker.service;

import com.lagou.worker.dao.LagouAuthCodeDao;
import com.lagou.worker.model.LagouAuthCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName LagouAuthCodeService
 * @Description TODO
 * @Author xsq
 * @Date 2020/7/28 16:41
 **/
@Service
public class LagouAuthCodeService {

    @Autowired
    private LagouAuthCodeDao lagouAuthCodeDao;

    @Transactional(rollbackFor = Exception.class)
    public void setEmailCode(String email, String code) {
        LagouAuthCode lagouAuthCode = new LagouAuthCode();
        lagouAuthCode.setCode(code);
        lagouAuthCode.setEmail(email);
        lagouAuthCode.setCreatetime(new Date());
        lagouAuthCode.setExpiretime(new Date());
        lagouAuthCodeDao.save(lagouAuthCode);
    }

    /**
     * 根据邮箱获取最新的验证码
     * @param email
     */
    public LagouAuthCode validate(String email) {
        return lagouAuthCodeDao.getLagouAuthCodeByEmail(email);
    }
}
